package com.hmyuan.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.hmyuan.dao.SysDictionaryDataMapper;
import com.hmyuan.domain.SysDictionaryData;
/**
 * 字典util
 * @author wbtianhl
 *
 */
public class DictionaryUtil {
	@Resource
	SysDictionaryDataMapper sysDictionaryDataMapper;

	public static Map<String, Map<String, String>> dictionary = new HashMap<>();
	
    //检查配置如果为空执行初始化
    public Map<String, String> checkAndInit(String type){
    	Map<String, String> typeData = dictionary.get(type);
    	if(typeData==null){
    		typeData = init(type);
    	}
    	return typeData;
    }
    //直接初始化
    public Map<String, String> init(String type){
    	SysDictionaryData sysDictionaryData = new SysDictionaryData();
		sysDictionaryData.setTypecode(type);
		List<SysDictionaryData> list = sysDictionaryDataMapper.selectWithType(sysDictionaryData);
		Map<String, String> typeData = new HashMap<>();
		for (SysDictionaryData sdd : list) {
			typeData.put(sdd.getName(), sdd.getValue());
		}
		dictionary.put(type, typeData);
    	return typeData;
    }
}
